//统计页面跳转到详细页面
var showReportOverSpeedDetail,
    passParams,//统计页面传递过来的参数
    locationDetailInfoDialog,//详细地址
    trajectoryReplayDialogId,
    showLocationDetailInfo;
var itemSearch;
var overspeedWarn;
//跳转到驾驶员信息页面
var godrvAnaDriverInfo;
//跳转到线路信息页面
var goLineInfo;
//跳转到车辆信息页面
var goCarInfo;

var captainTodaySearch;

require(['grid', 'topBar', 'layout', 'date'], function () {
    $('#top').topBar({
        left: [
            {
                type: 'filter-more',
                dateType: 'custom',
                search: function () {
                    search();
                },
                item: [
                    {
                        text: '时间快捷',
                        name1: 'beginTime',
                        name2: 'endTime',
                        type: 'dateLine'
                    }, {
                        text: '严重度',
                        dict: "PARA0100",
                        name: 'spedType',
                        type: 'item-select',
                        data: [
                            {text: '全部', value: ''}
                        ]
                    }
                ]
            }
        ],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                            exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                            exportFile('Pdf');
                        }
                    }
                ]
            },
            {
                text: '超速位置查询',
                type: 'btn',
                click: function () {
                    var ids = $('#grid').jqGrid("getGridParam", "selarrrow");
                    if (ids && ids.length > 0) {
                        var data = new Array();  // 在这里构造车辆数据的数组
                        for (var i = 0; i < ids.length; i++) {
                            var rowData = $('#grid').jqGrid("getRowData", ids[i]);
                            var obj = new Object();  //车辆数据
                            obj.licPltNo = rowData.licPltNo; //车牌号
                            obj.driverName = rowData.drvName; //驾驶员
                            obj.loLgt = rowData.beginLoLgt; //经度
                            obj.loLtt = rowData.beginLoLtt; //纬度
                            obj.direction = rowData.beginLoDrc; //方向
                            obj.type = "超速报警"; //异常类型 字符串表示
                            obj.beginTime = rowData.spdingBeginTime; //开始时间
                            obj.endTime = rowData.spdingEndTime; //结束时间
                            data.push(obj);
                        }
                        openLocationDetailInfoDialog("超速位置查询", data); //弹出的标题和数据
                    } else {
                        $.messager.alert("提示", "请勾选要查询的超速信息!", "info");
                    }
                }
            }
        ]
    });

    function exportFile(suffix) {
        if ($('#grid').getGridParam("reccount") > 0) {
            var url = "/overspeed/exportOverspeed" + suffix + ".htm";
            if (passParams) {
                $.exportFile(url, passParams);
            } else {
                var type = commonFun.getLeftTreeCurrentTreeType();
                var orgIds = commonFun.getLeftTreeCheckedNodes(type);
                if (!orgIds) {
                    $.messager.alert("提示", "请选择查询车辆/机构/驾驶员。", "info");
                } else {
                    var $postData = $("#top").parent().getValues();
                    $postData.type = type;
                    $postData.orgIds = orgIds;
                    $.exportFile(url, $postData);
                }
                ;
            }
            ;
        } else {
            $.messager.alert("提示", "请先查询数据。", "info");
        }
        ;
    }

    //grid初始化
    function gridInit() {
        $('#grid').grid({
            url: "/overspeed/searchOverspeedPage.htm",
            multiselect: true,
            colModel: [
                {name: "rcrdId", label: "rcrdId", key: true, hidden: true},
                {name: "carId", label: "carId", hidden: true},
                {name: "corpId", label: "corpId", hidden: true},
                {name: "lineId", label: "lineId", hidden: true},
                {name: "drvId", label: 'drvId', hidden: true},
                {name: "spdingBeginTime", label: "spdingBeginTime", hidden: true},
                {name: "spdingEndTime", label: "spdingEndTime", hidden: true},
                {name: "beginLoLgt", label: "beginLoLgt", hidden: true},
                {name: "beginLoLtt", label: "beginLoLtt", hidden: true},
                {name: "beginLoDrc", label: "beginLoDrc", hidden: true},
                {name: "organName", label: "organName", hidden: true},
                {name: "codeClDsc", label: "codeClDsc", hidden: true},
                {name: "codeDsc", label: "codeDsc", hidden: true},
                {name: "drvName", label: "驾驶员", width: "70",formatter: 'customCurrency',formatoptions:{
	                redbtn:true,
	                css:'text-link c-blue',
	                clickFn:function(rowObject,cellvalue){
	                	godrvAnaDriverInfo(rowObject.drvId);
	                }
	            }},
                {name: "licPltNo", label: "车牌号", width: "70",formatter: function (cellvalue,options,rowObject) {
                	return "<b><a href='javascript:void(0);' onclick='goCarInfo(\"" + rowObject.carId + "\")' class='text-link c-blue'>" + cellvalue + "</a></b>";
                }},
                {name: "carInCd", label: "车辆编号", width: "70"},
                {name: "lineName", label: "营运线路", width: "70",formatter: 'customCurrency',formatoptions:{
	                redbtn:true,
	                css:'text-link c-blue',
	                clickFn:function(rowObject,cellvalue){
	                	goLineInfo(rowObject.lineId);
	                }
	            }},
                {name: "organName", label: "所属机构", width: "80"},
                {name: "speedBeginValue", label: "超速开始值(KM/H)", align: "right", width: "110", formatter: "currency"},
                {
                    name: "spdingPrdSpeedAvrgValue",
                    label: "超速平均值(KM/H)",
                    align: "right",
                    width: "110",
                    formatter: "currency"
                },
                {
                    name: "spdingPrdSpeedMaxVal",
                    label: "超速期间速度最大值(KM/H)",
                    align: "right",
                    width: "120",
                    formatter: "currency"
                },
                {name: "spdingLitVal", label: "限速值(KM/H)", align: "right", width: "100", formatter: "currency"},
                {name: "spdingPercent", label: "超速百分比", align: "right", width: "80"},
                {name: "spdingSeverity", label: "严重度", width: "80", dict: "PARA0100"},
                {name: "spdingTimeLong", label: "超速时长", width: "80"},
                {name: "spdingType", label: "超速类型", width: "80",
                    formatter: function (cellvalue, options, rowObject) {
                        var result = "";
                        if(cellvalue=='0'){
                            result = '直行超速';
                        }else if(cellvalue=='1'){
                            result = '弯道超速';
                        }
                        return result;
                    }
                },
                {name: "rcrdTime", label: "发生时间", width: "130"},
                {
                    name: "rcrdPlace", label: "发生站点", width: "130",
                    formatter: function (cellvalue, options, rowObject) {
                        var licPltNo = rowObject.licPltNo;
                        var drvName = rowObject.drvName;
                        var beginLoLgt = rowObject.beginLoLgt;
                        var beginLoLtt = rowObject.beginLoLtt;
                        var beginLoDrc = rowObject.beginLoDrc;
                        var spdingBeginTime = rowObject.spdingBeginTime;
                        var spdingEndTime = rowObject.spdingEndTime;
                        var content = "<b><a href='javascript:void(0);' onclick='showLocationDetailInfo(\"" +
                            licPltNo + "\",\"" +
                            drvName + "\",\"" +
                            beginLoLgt + "\",\"" +
                            beginLoLtt + "\",\"" +
                            beginLoDrc + "\",\"车辆超速类型\",\"" +
                            spdingBeginTime + "\",\"" +
                            spdingEndTime + "\")' style='color: blue;'>" + cellvalue + "</a></b>";
                        return content;
                    }
                },
                {
                    name: "trackPlayback", label: "轨迹回放", width: "100", align: "center",
                    formatter: function (cellvalue, options, rowObject) {
                        var spedBeginTime = rowObject.spdingBeginTime,
                            spedEndTime = rowObject.spdingEndTime,
                            carId = rowObject.carId,
                            corpId = rowObject.corpId,
                            lineId = rowObject.lineId,
                            name = rowObject.organName,
                            province = rowObject.codeClDsc,
                            city = rowObject.codeDsc;
                        var content = '<a href ="javascript:;" class="dia_gj" onclick="showSpedTraj(\'' + carId + '\',\'' + spedBeginTime + '\',\'' + spedEndTime + '\',\'' + corpId + '\',\'' + lineId + '\',\'' + name + '\',\'' + province + '\',\'' + city + '\')">'
                            + "<img src = \'" + $.getProjectName() + "/content/images/track_ico.png'\' alt =''/>"
                            + '</a>';
                        return content;
                    }
                }
            ],
            showPaper: true,
            gridComplete: function () {
                var rowIds = $('#grid').jqGrid('getDataIDs');
                var cssprop = "";
                for (var i = 0; i < rowIds.length; i++) {
                    var rowId = rowIds[i];
                    var rowObject = $('#grid').jqGrid('getRowData', rowId);
                    if (rowObject.spdingSeverity == '严重超速') {
                        cssprop = {color: "#FF100B"};
                    } else if (rowObject.spdingSeverity == '一般超速') {
                        cssprop = {color: "#F97557"};
                    } else if (rowObject.spdingSeverity == '警告超速') {
                        cssprop = {color: "#F69F24"};
                    } else if (rowObject.spdingSeverity == '其它警告') {
                        cssprop = {color: "#F6E344"};
                    }
                    $('#grid').jqGrid("setCell", rowId, "spdingSeverity", rowObject.spdingSeverity, cssprop);
                }
                var rowIds = $('#grid').jqGrid('getDataIDs');
                var cells = $('#grid').find('td[aria-describedby="grid_rcrdPlace"]');
                $.each(cells, function (i, obj) {
                    var title = $('#grid').jqGrid('getCell', rowIds[i], 7) + "," + $('#grid').jqGrid('getCell', rowIds[i], 8);
                    $(obj).attr('title', title);
                });
            }
        });
    }

    showReportOverSpeedDetail = function (data) {
        passParams = data;
        $('.topBar').find('[name=beginTime]').val(data.beginTime);
        $('.topBar').find('[name=endTime]').val(data.endTime);
        $('#grid').grid('setGridParam', {
            page: 1,
            datatype: "json",
            postData: passParams
        }).trigger("reloadGrid");
    };

    //-- 显示 详细位置信息
    showLocationDetailInfo = function (licPltNo, drvName, beginLoLgt, beginLoLtt, beginLoDrc, type, spedBeginTime, spedEndTime) {
        if (drvName == "undefined") {
            drvName = "未知";
        }
        var data = new Array();  										// 在这里构造车辆数据的数组
        var obj = new Object();  										//车辆数据
        obj.licPltNo = licPltNo;	 										//车牌号
        obj.driverName = drvName; 									//驾驶员
        obj.loLgt = beginLoLgt; 											//经度
        obj.loLtt = beginLoLtt; 											//纬度
        obj.direction = beginLoDrc; 									//方向
        obj.type = type; 													//异常类型 字符串表示
        obj.beginTime = spedBeginTime; 							//开始时间
        obj.endTime = spedEndTime; 									//结束时间
        data.push(obj);
        openLocationDetailInfoDialog("超速位置查询", data); //弹出的标题和数据
    };

    function openLocationDetailInfoDialog(title, data) {
        if (!locationDetailInfoDialog) {
            locationDetailInfoDialog = $('<div>').dialog({
                title: title,
                url: './common/map/mapLocation.htm',
                height: $(window.parent).height() * 0.8,
                width: $(window.parent).width() * 0.85
            });
        }
        locationDetailInfoDialog.dialog('open');
        locationDetailInfoDialog.dialog('exec', 'showMapLocation', data);
    }

    /**
     * 打开轨迹回放弹窗
     */
    showSpedTraj = function (carId, beginTime, endTime, corpId, lineId, name, province, city) {
        var data = new Array();  // 在这里构造车辆数据的数组
        var obj = new Object();  //车辆数据
        obj.carId = carId;
        obj.beginTime = beginTime;
        obj.endTime = endTime;
        data.push(obj);
        if (!trajectoryReplayDialogId) {
            trajectoryReplayDialogId = $('<div>').dialog({
                title: '轨迹回放',
                url: './common/carAnaCarTrack.htm',
                height: $(window.parent).height() * 0.8,
                width: $(window.parent).width() * 0.85
            });
        }
        trajectoryReplayDialogId.dialog('open');
        trajectoryReplayDialogId.dialog('exec', 'loadData', carId, beginTime, endTime, name, province, city);
    };

    function search() {
        passParams = null;
        var $postData = $("#top").parent().getValues();
        // 左侧树tab选中的类型
        var type = commonFun.getLeftTreeCurrentTreeType();
        var orgIds = commonFun.getLeftTreeCheckedNodes(type);
        if (!orgIds) {
            $.messager.alert("提示", "请选择查询车辆/机构/驾驶员！", "info");
        } else {
            $postData.type = type;
            $postData.orgIds = orgIds.toString();
            $('#grid').grid('setGridParam', {
                datatype: "json",
                postData: $postData
            }).trigger("reloadGrid");
        }
    }

    itemSearch = function (data) {
        $('#grid').grid('setGridParam', {
            page: 1,
            datatype: "json",
            postData: {
                "orgIds": data.lineId,
                "type": "LINE",
                "beginTime": data.today,
                "endTime": data.today + " 23:59:59"
            }
        }).trigger("reloadGrid");
    };

    overspeedWarn = function () {
        var today = (new Date()).format();
        var Ids = [];
        var node = commonFun.getNodesByType("CAR");
        if (node && node.length > 0) {
            $.each(node, function (index, item) {
                Ids.push(item.id);
            });
            $('#grid').grid('setGridParam', {
                page: 1,
                datatype: "json",
                postData: {
                    "orgIds": Ids,
                    "type": "CAR",
                    "beginTime": today,
                    "endTime": today + " 23:59:59"
                }
            }).trigger("reloadGrid");
        }
    };
    
  //跳转到驾驶员信息页面
    godrvAnaDriverInfo = function(drvId){
    	var data = {};
    	data.drvId = drvId;
    	$.openTab('8252b8b5e5ff49c08b02f6865562b506','showDrvInfo',[data]);
    };
    //跳转到车辆信息页面
    goCarInfo = function(carId){
    	var data = {};
    	data.carId = carId;
    	$.openTab('6bad880fef164ec6adaea7b18420eb6e','showCarInfo',[data]);
    }
    //跳转到线路信息页面
    goLineInfo = function(lineId){
    	var data = {};
    	data.lineId = lineId;
    	$.openTab('4db17f8a322b4749a61eb2f8a12023ae','showLineInfo',[data]);
    }

    captainTodaySearch = function(data){
        //切换成线路
        commonFun.showLeftTree(commonFun.lineTree);
        //勾选并展开节点
        commonFun.setCheckedNodes(data.lineIds);
        $("div[name='spedType']").find("a[data-val='']").click();
        $("a[data-val='01']").click();
    };

    $('body').layout();
    //初始化grid
    gridInit();
});
